﻿using System;
using System.Diagnostics;

namespace Runtime.Services.Battle.Entity {

    /// <summary>
    /// 战斗实体实例ID
    /// </summary>
    [DebuggerStepThrough]
    [DebuggerNonUserCode]
    public readonly struct DFR_BattleEntityInstanceID {

        internal static uint COUNT = 1;

        /// <summary>
        /// 空值
        /// </summary>
        public static readonly DFR_BattleEntityInstanceID Empty = new();

        private readonly DFR_BattleEntityID m_ID;

        /// <summary>
        /// ID
        /// </summary>
        public DFR_BattleEntityID ID {
            get => m_ID;
            init {
                m_ID = value;
                m_Index = COUNT++;
                m_GUID = Guid.NewGuid();
                m_HashCode = m_GUID.GetHashCode();
            }
        }

        private readonly uint m_Index;

        private DFR_BattleEntityInstanceID(DFR_BattleEntityID id, Guid guid, uint index) {
            m_ID = id;
            m_Index = index;
            m_GUID = guid;
            m_HashCode = m_GUID.GetHashCode();
        }

        private DFR_BattleEntityInstanceID(DFR_BattleEntityID id, Guid guid) {
            m_ID = id;
            m_Index = COUNT++;
            m_GUID = guid;
            m_HashCode = m_GUID.GetHashCode();
        }

        public DFR_BattleEntityInstanceID(DFR_BattleEntityID id) {
            m_ID = id;
            m_Index = COUNT++;
            m_GUID = Guid.NewGuid();
            m_HashCode = m_GUID.GetHashCode();
        }

        /// <summary>
        /// 生产索引号
        /// </summary>
        public uint Index {
            get => m_Index;
        }

        private readonly Guid m_GUID;
        private readonly int m_HashCode;

        /// <summary>
        /// GUID
        /// </summary>
        public Guid GUID {
            get => m_GUID;
        }

        public override int GetHashCode() => m_HashCode;

        public override readonly bool Equals(object obj) {
            if (obj is DFR_BattleEntityInstanceID other) {
                return other.m_GUID == m_GUID;
            }
            return false;
        }

        public override string ToString() => $"[{nameof(DFR_BattleEntityInstanceID)}({nameof(ID.Type)}={ID.Type},{nameof(ID.TableID)}={ID.TableID},{nameof(GUID)}={m_GUID})]";

        public static bool operator ==(DFR_BattleEntityInstanceID left, DFR_BattleEntityInstanceID right) => left.Equals(right);

        public static bool operator !=(DFR_BattleEntityInstanceID left, DFR_BattleEntityInstanceID right) => !left.Equals(right);
    }
}
